package models

import (
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"token-for-token/global"
)

type ChainApply struct {
	Id             primitive.ObjectID `bson:"_id ,omitempty"`
	ChainConfigId0 string             `bson:"chainConfigId0"`
	ChainConfigId1 string             `bson:"chainConfigId1"`
	TxHash         string             `bson:"txHash"`
	From           string             `bson:"from"`
	To             string             `bson:"to"`
	Status         string             `bson:"status"`
	UserId         string             `bson:"userId"`
	TokenId0       string             `bson:"tokenId0"`
	TokenId1       string             `bson:"tokenId1"`
}

func (c *ChainApply) GetChainApplyInfoForId(hash string) error {
	user := global.DBEngine.Database(global.DbConfig.DbName).Collection("chain_apply")
	filter := bson.M{"txHash": hash, "status": "0"}
	err := user.FindOne(context.Background(), filter).Decode(&c)
	return err
}

func (c *ChainApply) UpdateChainApplyStatus(status string) error {
	user := global.DBEngine.Database(global.DbConfig.DbName).Collection("chain_apply")
	filter := bson.M{"txHash": c.TxHash}
	update := bson.M{"$set": bson.M{"status": status}}
	_, err := user.UpdateOne(context.Background(), filter, update)
	return err
}
